package com.tuling.mybaitis_spring;

import com.tuling.annotation.MybatisMapperScan;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

import java.io.IOException;
import java.util.Map;
public class MybatisBeanDefinitionRegister implements ImportBeanDefinitionRegistrar {

	@Override
	public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
		//扫描指定包下的mapper文件 1、定义包扫描工具 2、修改beanDefinition 筛选规则
		Map<String, Object> annotationAttributes = annotationMetadata.getAnnotationAttributes(MybatisMapperScan.class.getName());
		String path = (String)annotationAttributes.get("value");
		System.out.println("xxx"+path);

		MybatisClassPathBeanDefinitionScanner scanner = new MybatisClassPathBeanDefinitionScanner(registry);
		scanner.addIncludeFilter(new TypeFilter() {
			@Override
			public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
				return true;
			}
		});
		scanner.doScan(path);

		ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(annotationMetadata, registry);
	}
}
